//
//  CustomNavigationController.m
//  BountyWaiter
//
//  Created by ZhenYu Wang on 2024/1/12.
//

#import "CustomNavigationController.h"
#import "CustomerNavigationBar.h"
#import "UINavigationBar+Awesome.h"

@interface CustomNavigationController ()

@end

@implementation CustomNavigationController

- (instancetype)init{
    if (self = [super initWithNavigationBarClass:[CustomerNavigationBar class] toolbarClass:nil]) {
        self.fd_fullscreenPopGestureRecognizer.enabled = NO;
        if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
            // 背景色
            appearance.backgroundColor = [UIColor whiteColor];
            // 去掉半透明效果
            appearance.backgroundEffect = nil;
            // 标题字体颜色及大小
            appearance.titleTextAttributes = @{
                NSForegroundColorAttributeName : TextColor,
                NSFontAttributeName : [UIFont systemFontOfSize:18.0 weight:UIFontWeightMedium],
            };
            // 设置导航栏下边界分割线透明
            appearance.shadowImage = [[UIImage alloc] init];
            // 去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
            appearance.shadowColor = [UIColor clearColor];
            // standardAppearance：常规状态, 标准外观，iOS15之后不设置的时候，导航栏背景透明
            self.navigationBar.standardAppearance = appearance;
            // scrollEdgeAppearance：被scrollview向下拉的状态, 滚动时外观，不设置的时候，使用标准外观
            self.navigationBar.scrollEdgeAppearance = appearance;
        } else {
            [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:18.0 weight:UIFontWeightMedium]}];
            self.navigationBar.tintColor = TextColor;
            self.navigationBar.barStyle = UIBarStyleDefault;
            [self.navigationBar setShadowImage:getColorToImage([UIColor clearColor])];
            [self.navigationBar setTranslucent:YES];
            [self.navigationBar lt_setBackgroundImage:getColorToImage([UIColor whiteColor])];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置右滑返回手势的代理为自身
    __weak typeof(self) weakself = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakself;
    }
}

#pragma mark - UIGestureRecognizerDelegate
//这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        //屏蔽调用rootViewController的滑动返回手势，避免右滑返回手势引起死机问题
        if (self.viewControllers.count < 2 ||
            self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    //这里就是非右滑手势调用的方法啦，统一允许激活
    return YES;
}
@end
